<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openDepart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="closeDepart"
      @open="openDialog"
    >
      <template #default>
        <el-form ref="Form" label-width="120px" :model="formList" :rules="rules">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="formList.name" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门编码" prop="code">
            <el-input v-model="formList.code" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门负责人" prop="manager">
            <el-select v-model="formList.manager" style="width:80%" placeholder="请选择">
              <el-option
                v-for="item in managerList"
                :key="item.id"
                :label="item.username"
                :value="item.username"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍" prop="introduce">
            <el-input v-model="formList.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDepart">取 消</el-button>
          <el-button type="primary" @click.prevent="submitForm">确 定</el-button>
        </span>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import { delDepartAPI, fetchDepertmentListAPI, addManagerAPI, updateFormListAPI } from '@/api/department'
import { transTree } from '@/utils/transTree'
export default {
  data() {
    const checkCodes = async(rule, value, callback) => {
      let arrList = []
      if (!this.formList.id) {
        arrList = this.codelist
      } else {
        this.codelist.forEach(item => {
          if (item !== this.codeid) {
            arrList.push(item)
          }
        })
      }
      if (arrList.includes(value)) {
        callback(new Error('部门编码已被占用'))
      }
      callback()
    }
    return {
      codeid: '',
      codelist: [],
      managerList: [],
      formList: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码由1-50个字符组成', trigger: 'blur' },
          { validator: checkCodes, trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '部门介绍必填', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍由1-300个字符组成', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人必填', trigger: 'change' }
        ],
        name: [
          { required: true, message: '部门名称必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称由1-50个字符组成', trigger: 'blur' }
        ]
      },
      list: [],
      defaultProps: {
        label: 'name'
      },
      dialogVisible: false

    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 置空formlist数据
    clearFormlist() {
      this.formList = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: '',
        id: ''
      }
    },
    // 获取页面
    async getList() {
      const res = await fetchDepertmentListAPI()
      res.depts.forEach(item => {
        this.codelist.push(item.code)
      })
      this.list = transTree(res.depts)
    },
    // 打开
    openDepart(id) {
      this.dialogVisible = true
      this.formList.pid = id
    },
    // 关闭回调
    closeDepart() {
      this.$refs.Form.resetFields()
      this.clearFormlist()
      this.dialogVisible = false
    },
    // 打开回调
    async openDialog() {
      const res = await this.$http.get('/sys/user/simple')
      this.managerList = res
    },
    // 点击编辑或添加部门
    submitForm() {
      this.$refs.Form.validate(async(valid) => {
        if (valid) {
          if (this.formList.id) {
            await updateFormListAPI(this.formList)
          } else {
            await addManagerAPI(this.formList)
          }
          this.$message.success(`${this.formList.id ? '编辑成功' : '添加成功'}`)
          this.dialogVisible = false
          this.getList()
          this.$refs.Form.resetFields()
          this.clearFormlist()
        }
      })
    },
    // 编辑数据回填
    async editDepart(id) {
      this.dialogVisible = true
      const res = await this.$http.get(`/company/department/${id}`)
      this.formList = res
      this.codeid = res.code
    },
    // 删除
    delDepart(id) {
      this.$confirm('确认删除此部门吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDepartAPI(id)
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style>
</style>
